package com.hanbang.gycatering.classify;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.CommentData;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.myviews.ColoredRatingBar;
import com.hanbang.gycatering.myviews.ScaleImageView;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.JsonHelper;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.PagingHelp;
import com.hanbang.gycatering.utils.SystemTools;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class CommentActivity extends BaseActivity {
	private int id = -1;
	private PagingHelp pagingHelp = new PagingHelp();
	private MyAdapter adapter;
	private PullToRefreshListView listView = null;
	private ArrayList<CommentData> commentDatas = new ArrayList<CommentData>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment);
		id = getIntent().getIntExtra("id", id);
		initView();
		setTheClick();
		getCommentList();
	}

	private void initView() {
		((TextView) findViewById(R.id.top_bar_title)).setText("商品评论");
		// 设置返回的点击事件
		super.setBackArrows(((ImageView) findViewById(R.id.top_bar_left_image)));
		listView = (PullToRefreshListView) findViewById(R.id.listview);
		adapter = new MyAdapter();
		listView.setAdapter(adapter);
		listView.setMode(Mode.BOTH);
		ListView ll = listView.getRefreshableView();
		// 显示头部出现分割线
		ll.setHeaderDividersEnabled(false);
		// 禁止底部出现分割线
		ll.setFooterDividersEnabled(false);
		ll.setDivider(null);
		ll.setDividerHeight(SystemTools.dip2px(this, 10));
		listView.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				pagingHelp.clear();
				commentDatas.clear();
				getCommentList();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				getCommentList();
			}
		});

	}

	private void getCommentList() {
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.commentlist + "&id=" + id
						+ "&PageIndex=" + pagingHelp.getPageindex(),
				new MyRequestCallBack<String>(CommentActivity.this,
						getString(R.string.loadding)) {
					@Override
					public void onFailure(HttpException error, String msg) {
						super.onFailure(error, msg);
						listView.onRefreshComplete();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							int state = json.getInt("state");
							if (state == 1) {
								JSONArray info = json.getJSONArray("info");
								pagingHelp.setRecordCount(json
										.getInt("recordCount"));
								pagingHelp.setPageCount(json
										.getInt("pageCount"));
								int i;
								for (i = pagingHelp.getCurrentCount(); i < info
										.length(); i++) {
									CommentData data = JsonHelper.parseObject(
											info.getJSONObject(i),
											CommentData.class);
									if (data != null) {
										commentDatas.add(data);
									}
								}
								pagingHelp.isNextPaging(CommentActivity.this,
										info.length(), i);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
						adapter.notifyDataSetChanged();
						listView.onRefreshComplete();
					}
				});
	}

	private void setTheClick() {
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return commentDatas.size();
		}

		@Override
		public Object getItem(int position) {
			return commentDatas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@SuppressLint("InflateParams")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			CommentData data = commentDatas.get(position);
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = getLayoutInflater().inflate(
						R.layout.comment_list_item, null);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.coloredRatingBar = (ColoredRatingBar) convertView
						.findViewById(R.id.coloredRatingBar);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.content = (TextView) convertView
						.findViewById(R.id.content);
				holder.imgs = (LinearLayout) convertView
						.findViewById(R.id.imgs);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.name.setText(data.getName());
			holder.coloredRatingBar.setRating(data.getStar());
			holder.time.setText(data.getDate());
			holder.content.setText(data.getContent());
			if ((data.getImg1() == null || data.getImg1().equals(""))
					&& (data.getImg2() == null || data.getImg2().equals(""))
					&& (data.getImg3() == null || data.getImg3().equals(""))) {
				holder.imgs.setVisibility(View.GONE);
			} else {
				holder.imgs.setVisibility(View.VISIBLE);
				if (data.getImg1() != null && !data.getImg1().equals("")) {
					holder.imgs.getChildAt(0).setVisibility(View.VISIBLE);
					bitmapUtils.display(
							((ScaleImageView) holder.imgs.getChildAt(0)),
							SystemTools.getHttpFileUrl(data.getImg1()));
					((ScaleImageView) holder.imgs.getChildAt(0))
							.setOnClickListener(new MyOnClickListener(0,
									position));
				} else {
					holder.imgs.getChildAt(0).setVisibility(View.INVISIBLE);
				}
				if (data.getImg2() != null && !data.getImg2().equals("")) {
					holder.imgs.getChildAt(1).setVisibility(View.VISIBLE);
					bitmapUtils.display(
							((ScaleImageView) holder.imgs.getChildAt(1)),
							SystemTools.getHttpFileUrl(data.getImg2()));
					((ScaleImageView) holder.imgs.getChildAt(1))
							.setOnClickListener(new MyOnClickListener(1,
									position));
				} else {
					holder.imgs.getChildAt(1).setVisibility(View.INVISIBLE);
				}
				if (data.getImg3() != null && !data.getImg3().equals("")) {
					holder.imgs.getChildAt(2).setVisibility(View.VISIBLE);
					bitmapUtils.display(
							((ScaleImageView) holder.imgs.getChildAt(2)),
							SystemTools.getHttpFileUrl(data.getImg3()));
					((ScaleImageView) holder.imgs.getChildAt(2))
							.setOnClickListener(new MyOnClickListener(2,
									position));
				} else {
					holder.imgs.getChildAt(2).setVisibility(View.INVISIBLE);
				}
			}
			return convertView;
		}

		private class ViewHolder {
			TextView name = null;
			ColoredRatingBar coloredRatingBar = null;
			TextView time = null;
			TextView content = null;
			LinearLayout imgs = null;
		}
	}

	private class MyOnClickListener implements OnClickListener {
		int current = 0;
		ArrayList<String> list = new ArrayList<String>();

		public MyOnClickListener(int current, int position) {
			CommentData data = commentDatas.get(position);
			this.current = current;
			if (data.getImg1() != null && !data.getImg1().equals("")) {
				list.add(data.getImg1());
				if (data.getImg2() != null && !data.getImg2().equals("")) {
					list.add(data.getImg2());
					if (data.getImg3() != null && !data.getImg3().equals("")) {
						list.add(data.getImg3());
					}
				}
			}

		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(CommentActivity.this, ImageBrowseActivity.class);
			intent.putStringArrayListExtra("imgs", list);
			intent.putExtra("current", current);
			startActivity(intent);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();

	};

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
